import { Component, OnInit ,Input ,Output, EventEmitter, Host, Inject, forwardRef} from '@angular/core';
import {NbDialogService, NbToastrService} from '@nebular/theme';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';
import {LocalDataSource} from 'ng2-smart-table';
import {FoodService} from '../../service/food.service';
import {ViewCell} from 'ng2-smart-table';
import {HttpClient} from '@angular/common/http';
import {ReturnData} from '../../login/login.component';
import {SERVER_URL} from '../../config';

@Component({
  selector: 'button-view',
  template: `
    <button nbButton class="btn-primary" (click)="onClick()">审批</button>
  `,
})
export class ButtonApprovalComponent implements ViewCell, OnInit {
  renderValue: string;

  @Input() value: string;
  @Input() rowData: any;

  @Output() save: EventEmitter<any> = new EventEmitter();

  constructor(
    private http: HttpClient,
    @Host() @Inject(forwardRef(() => ManufactureComponent)) private app: ManufactureComponent) {
  }

  ngOnInit() {
    this.renderValue = this.value.toString().toUpperCase();
  }

  onClick() {
    this.save.emit(this.rowData);
    this.app.approval(this.rowData.id);
  }
}

@Component({
  selector: 'app-manufacture',
  templateUrl: './manufacture.component.html',
  styleUrls: ['./manufacture.component.css'],
  entryComponents: [ButtonApprovalComponent],
})
export class ManufactureComponent implements OnInit {

  settings = {
    noDataMessage: '暂无数据',
    actions: {
      add: false,
      edit: false,
      delete: false,
      columnTitle: '审批',
    },
    columns: {
      id: {
        title: 'ID',
        type: 'number',
        editable: false,
      },
      Name: {
        title: '工厂名称',
        type: 'string',
      },
      Address: {
        title: '地址',
        type: 'string',
      },
      Tel: {
        title: '电话',
        type: 'string',
      },
      qsCode: {
        title: '生产许可证编号',
        type: 'string',
      },
      qsStartTime: {
        title: '生效时间',
        type: 'string',
      },
      qsEndTime: {
        title: '失效时间',
        type: 'string',
      },
      Fcondition: {
        title: '状态',
        editable: false,
        filter: {
          type: 'list',
          config: {
            selectText: '选择状态',
            list: [
              {value: '审核通过', title: '审核通过'},
              {value: '审核未通过', title: '审核未通过'},
            ]
          }
        }
      },
      button: {
        title: '操作',
        type: 'custom',
        renderComponent: ButtonApprovalComponent,
        onComponentInitFunction(instance) {
          instance.save.subscribe(row => {
            console.log(row);
          });
        },
        filter: false,
      },
    },
  };

  source: LocalDataSource = new LocalDataSource();
  public manufacture: any = [];

  constructor(private foodService: FoodService,
              private dialogService: NbDialogService,
              private toastrService: NbToastrService,
              private http: HttpClient) {
    this.loadData();
  }

  async loadData() {
    const formData = new FormData();
    this.manufacture = await this.foodService.getAll('?s=App.Manufacturer.Selectall', formData);
    console.log(this.manufacture);
    this.source.load(this.manufacture.data.code);
  }

  ngOnInit() {
  }

  approval(id) {
    const formData = new FormData();
    formData.append('id', id);
    this.http.post<ReturnData>(SERVER_URL + '?s=App.Manufacturer.Approval', formData).subscribe( data => {
      if (data.ret === 200) {
        this.toastrService.show('审批成功', '提示', {status: NbToastStatus.SUCCESS});
        this.loadData();
      } else {
        this.toastrService.show('审批失败', '提示', {status: NbToastStatus.DANGER});
      }
    });
  }

}
